package logic

import (
	"context"
	"fmt"
	"teacher1/model/mysql"
	"teacher1/studentrpc/internal/svc"
	"teacher1/studentrpc/studentrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StudentDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStudentDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StudentDelLogic {
	return &StudentDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StudentDelLogic) StudentDel(in *studentrpc.StudentDelRequest) (*studentrpc.StudentDelResponse, error) {
	// todo: add your logic here and delete this line
	var student mysql.Student
	//当前学员不存在 无法删除
	mysql.DB.Debug().Where("id=?", in.Id).Find(&student)
	if student.ID == 0 {
		return nil, fmt.Errorf("当前学员不存在")
	}
	if student.TeaCherId != in.TeacherId {
		return nil, fmt.Errorf("当前学员不在当前老师名下")
	}
	mysql.DB.Debug().Delete(&student)
	return &studentrpc.StudentDelResponse{}, nil
}
